Homework_Week4

Author

Pippin

Homework_Week 4

1.load packages

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.5.2
✔ ggplot2   4.0.0     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(here)
here() starts at D:/CASA/CASA0005
library(janitor)

Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(stringr)

2.read and pocess data

GiiData <- read_csv(here::here("Week4/Data",
           "HDR25_Composite_indices_complete_time_series.csv"))%>%
  clean_names()%>%
  select(c(1:2,4,656,665))%>%
  mutate(diff_10_19 = `gii_2010`-`gii_2019`)
Rows: 206 Columns: 1112
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr    (4): iso3, country, hdicode, region
dbl (1108): hdi_rank_2023, hdi_1990, hdi_1991, hdi_1992, hdi_1993, hdi_1994,...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
shp <- st_read(here::here("Week4/Data/World_Countries_(Generalized)_-573431906301700955",
                          "World_Countries_Generalized.shp"))%>%
  clean_names()
Reading layer `World_Countries_Generalized' from data source 
  `D:\CASA\CASA0005\Week4\Data\World_Countries_(Generalized)_-573431906301700955\World_Countries_Generalized.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 251 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -20037510 ymin: -30240970 xmax: 20037510 ymax: 18418390
Projected CRS: WGS 84 / Pseudo-Mercator
head(shp)
Simple feature collection with 6 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -19015950 ymin: -2039467 xmax: 8339582 ymax: 5260415
Projected CRS: WGS 84 / Pseudo-Mercator
         country iso    countryaff aff_iso                       geometry
1    Afghanistan  AF   Afghanistan      AF MULTIPOLYGON (((6821275 424...
2        Albania  AL       Albania      AL MULTIPOLYGON (((2178615 511...
3        Algeria  DZ       Algeria      DZ MULTIPOLYGON (((512443 4423...
4 American Samoa  AS United States      US MULTIPOLYGON (((-19007124 -...
5        Andorra  AD       Andorra      AD MULTIPOLYGON (((160949.7 52...
6         Angola  AO        Angola      AO MULTIPOLYGON (((2613349 -19...

2.1 use countrycode package convert iso format

countrycode( list, origin = ” “, destination =” “)

“iso2c” “iso3c” “country.name” “continent”

library(countrycode)
shp_iso3 <- shp%>%
  mutate(iso3 = iso)

shp_iso3$iso3 <-  shp_iso3$iso3%>%
  countrycode::countrycode(.,origin = "iso2c",destination = "iso3c")

3.join data

joint_data <- shp_iso3%>%
  left_join(.,GiiData,
            by = c("iso3"))%>%
  select(c(1,5,8,9,10))%>%
  mutate(across(where(is.numeric),~round(.x,2)))

4.plot

# plot
library(tmap)
library(tmaptools)

joint_data <- joint_data%>%
  st_transform(.,4326)

tmap_mode("view")
ℹ tmap modes "plot" - "view"
ℹ toggle with `tmap::ttm()`
tm_shape(joint_data)+
  tm_polygons(fill = "diff_10_19",
              fill.scale = tm_scale_intervals(values="-brewer.RdYlBu",
                                              style="jenks"),
              fill_alpha=0.4,
              fill.legend = tm_legend(title = "the index of inequality", 
                                      size = 0.6))+
  tm_basemap(server = "OpenStreetMap") +
  tm_compass(type = "arrow", position = c("left", "bottom")) + 
  tm_scalebar(position = c("left", "bottom"))+
  tm_title("The changes of inenquality index from 2010 to 2019", 
            size = 2,
            position = c("center", "top"))
[scale] tm_polygons:() the data variable assigned to 'fill' contains positive and negative values, so midpoint is set to 0. Set 'midpoint = NA' in 'fill.scale = tm_scale_intervals(<HERE>)' to use all visual values (e.g. colors)
[cols4all] color palettes: use palettes from the R package cols4all. Run
`cols4all::c4a_gui()` to explore them. The old palette name "-brewer.RdYlBu" is
named "rd_yl_bu" (in long format "brewer.rd_yl_bu")